**Factor analysis with group-specific structure
use "${base}\replication_file\Gallup_Family_Adolescent_Health_2023_Replication_File.dta", clear
cd "${output}"

encode RACE, gen(race)
gen race_string="other" if race==1
replace race_string="black" if race==2
replace race_string="hispanic" if race==3
replace race_string="other" if race==4
replace race_string="white" if race==5

**Restrict and standardize to adolescent pop
keep if age>=13
*****************

global neg_loadf1 "B10_14	B10_17	B10_15	B10_13"

**REVERSE CODE negative loaders**
foreach  i in $neg_loadf1 {
gen RE`i'=5 if `i'==1
replace RE`i'=4 if `i'==2
replace RE`i'=3 if `i'==3
replace RE`i'=2 if `i'==4
replace RE`i'=1 if `i'==5
}

*Pooled--all other groups
global f1 "B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7"
global f2 "REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18"
global f3 "B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe"
global f4 "C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol"
*Black
global Bf1 "B10_2	B10_4	B10_21	B10_6	B10_3	B10_1	B10_19	B10_5	B10_12	B10_16	B10_7"
global Bf2 "REB10_15	REB10_17	REB10_14	REB10_13"
global Bf3 "C19_parent_love	B9_relationship_othe	C24_safety	B6_overall_parent_ch	REB11A	B11B	REB10_8	NC21_angry"
*Hispanic
global Hf1  "B10_2	B10_21	B10_1	B10_4	B10_6	B10_3	B10_7	B10_19"
global Hf2  "REB10_13	REB10_15	REB10_17	B10_16"				
global Hf3 "B6_overall_parent_ch	C19_parent_love	REB11A	B9_relationship_othe	REB10_8	B11B"
global Hf4 "NC23a_rejection	NC23_left	C24_safety	NC20_alcohol	NC21_angry"	
*White
global Wf1 "B10_2	B10_5	B10_3	B10_6	B10_1	B10_19	B10_4	B10_21"
global Wf2 "B10_16	B10_18 REB10_14	REB10_17 REB10_15	REB10_13"
global Wf3 "B6_overall_parent_ch	REB11A	REB10_8	B11B	C19_parent_love"	
global Wf4 "NC23a_rejection	NC21_angry	NC23_left	NC20_alcohol	C24_safety"	

/*
B10_1	1.        I respond quickly to my child's needs
B10_10	10.     Physical punishment, such as spanking is sometimes necessary
B10_11	11.     My child doesn't need to understand the reasons for my rules
B10_12	12.     I give my child clear commands
B10_13	13.     My child often gets their way when we have a conflict
B10_14	14.     I don't always follow through on punishments
B10_15	15.     I have a hard time saying "no" to my child
B10_16	16.     I set well-established rules for my child.
B10_17	17.     I find it difficult to discipline my child
B10_18	18.     My child completes the priorities I set for them before they are allowed to play or relax
B10_19	19.     I encourage my child to think of others 
B10_2	2.        I encourage my child to talk about their feelings or problems
B10_20	20.     My child is frequently out of control
B10_21	21.     I teach my child to do the right thing, even when it is hard
B10_3	3.        I apologize if I act unfairly or too harshly toward my child
B10_4	4.        I hug or kiss my child every day
B10_5	5.        I consider my child's preferences before making decisions that affect them
B10_6	6.        I often joke and play with my child
B10_7	7.        I am usually easy going and relaxed when with my child
B10_8	8.        I argue frequently with my child

B11B   I share an affectionate, warm relationship with my child
REB10_8   I argue frequently with my child
REB11A   My child easily becomes angry at me
B6_overall_parent_ch   Which word or phrase below best describes the overall quality of your relationship with this child?
B9_relationship_othe   How would you describe the overall quality of the relationship between your child and the other parental figure you just identified?
C19_parent_love   On a scale of 0-10, where 0 is the weakest and least loving relationship you can imagine, and 10 is the strongest and most loving, how would you rate your relationship with your parents or the people who take care of you the most
C24_safety   How often do you feel safe and protected? 
NC20_alcohol   Do you live or have you lived with someone who struggles with drug or alcohol abuse?
NC21_angry   Do you live or have you lived with someone who is frequently angry?
NC23_left   Has a parent who had been living with you, stopped living with you? 
NC23a_rejection   Have you ever felt rejected or abandoned by your biological father or biological mother?
*/


*************
**Standardize to pooled sample**
*********
foreach x in comp_mental_health $f1 $f2 $f3 $f4 {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
}

egen rules=rowmean(B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7)
egen enforce=rowmean(REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18)
egen relate=rowmean(B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe)
egen adverse=rowmean(C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol)

foreach x in rules enforce  relate adverse  {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
} 
label var rules "Authoritative regulation"
label var enforce "Enforcement strength"
label var relate "Relationship quality"
label var adverse "Absence of traumatic experiences"

global controls "male age_group_num1 age_group_num2 age_group_num3 age_group_num4 age_group_num5 age_group_num6 age_group_num7 age_group_num8"

cor rules enforce [aw=weight]
cor adverse relate [aw=weight]

*********************
***Group Specific standardization**
*********

**BLACK
foreach x in comp_mental_health $Bf1 $Bf2 $Bf3 $Bf4 {
sum `x' if race==2 [aw=weight]
gen BM`x'=r(mean)
gen BS`x'=r(sd)
gen B`x'=(`x'-BM`x')/BS`x'
drop BM`x' BS`x'
}


**HISP
foreach x in comp_mental_health $Hf1 $Hf2 $Hf3 $Hf4 {
sum `x' if race==3 [aw=weight]
gen HM`x'=r(mean)
gen HS`x'=r(sd)
gen H`x'=(`x'-HM`x')/HS`x'
drop HM`x' HS`x'
}

**WHITE
foreach x in comp_mental_health $Wf1 $Wf2 $Wf3 $Wf4 {
sum `x' if race==5 [aw=weight]
gen WM`x'=r(mean) 
gen WS`x'=r(sd) 
gen W`x'=(`x'-WM`x')/WS`x' 
drop WM`x' WS`x'
}

**ALL OTHER RACES
foreach x in comp_mental_health $f1 $f2 $f3 $f4 {
sum `x' if race!=5 & race!=3 & race!=2 [aw=weight]
gen AM`x'=r(mean) 
gen AS`x'=r(sd) 
gen A`x'=(`x'-AM`x')/AS`x' 
drop AM`x' AS`x'
}



**Group-specific standardizations with group-specific structures
egen rules_B=rowmean(BB10_2	BB10_4	BB10_21	BB10_6	BB10_3	BB10_1	BB10_19	BB10_5	BB10_12	BB10_16	BB10_7)
egen enforce_B=rowmean(BREB10_15	BREB10_17	BREB10_14	BREB10_13)
egen relate_B=rowmean(BC19_parent_love	BB9_relationship_othe	BC24_safety	BB6_overall_parent_ch	BREB11A	BB11B	BREB10_8	BNC21_angry)
gen adverse_B=.

egen rules_H=rowmean(HB10_2	HB10_21	HB10_1	HB10_4	HB10_6	HB10_3	HB10_7	HB10_19)
egen enforce_H=rowmean(HREB10_13	HREB10_15	HREB10_17	HB10_16)
egen relate_H=rowmean(HB6_overall_parent_ch	HC19_parent_love	HREB11A	HB9_relationship_othe	HREB10_8	HB11B)
egen adverse_H=rowmean(HNC23a_rejection	HNC23_left	HC24_safety	HNC20_alcohol	HNC21_angry)

egen rules_W=rowmean(WB10_2	WB10_5	WB10_3	WB10_6	WB10_1	WB10_19	WB10_4	WB10_21)
egen enforce_W=rowmean(WB10_16	WB10_18 WREB10_14	WREB10_17 WREB10_15	WREB10_13)
egen relate_W=rowmean(WB6_overall_parent_ch	WREB11A	WREB10_8	WB11B	WC19_parent_love)
egen adverse_W=rowmean(WNC23a_rejection	WNC21_angry	WNC23_left	WNC20_alcohol	WC24_safety)

foreach i in B H W A {
gen comp_mental_health_`i'=`i'comp_mental_health
}

foreach i in A {
egen rules_`i'=rowmean(`i'B10_2	`i'B10_6	`i'B10_3	`i'B10_1	`i'B10_5	`i'B10_19	`i'B10_4	`i'B10_21	`i'B10_7)
egen enforce_`i'=rowmean(`i'REB10_14	`i'REB10_17 `i'REB10_15	`i'REB10_13	`i'B10_16	`i'B10_18)
egen relate_`i'=rowmean(`i'B6_overall_parent_ch `i'B11B	`i'REB11A	`i'REB10_8	`i'C19_parent_love	`i'B9_relationship_othe)
egen adverse_`i'=rowmean(`i'C24_safety	`i'NC21_angry	`i'NC23a_rejection	`i'NC23_left	`i'NC20_alcohol)
}

**Standardize factors to each group
foreach y in rules  relate adverse enforce {
foreach r in B  {
sum `y'_`r' if race==2 [aw=weight]
gen M`y'_`r'=r(mean)
gen S`y'_`r'=r(sd)
gen FIX`y'_`r'=(`y'_`r'-M`y'_`r')/S`y'_`r'
replace `y'_`r'=FIX`y'_`r' if race==2
drop FIX`y'_`r' M`y'_`r' S`y'_`r'
}
}

foreach y in rules  relate adverse enforce {
foreach r in H  {
sum `y'_`r' if race==3 [aw=weight]
gen M`y'_`r'=r(mean)
gen S`y'_`r'=r(sd)
gen FIX`y'_`r'=(`y'_`r'-M`y'_`r')/S`y'_`r'
replace `y'_`r'=FIX`y'_`r' if race==3
drop FIX`y'_`r' M`y'_`r' S`y'_`r'
}
}
foreach y in rules  relate adverse enforce {
foreach r in W  {
sum `y'_`r' if race==5 [aw=weight]
gen M`y'_`r'=r(mean)
gen S`y'_`r'=r(sd)
gen FIX`y'_`r'=(`y'_`r'-M`y'_`r')/S`y'_`r'
replace `y'_`r'=FIX`y'_`r' if race==5
drop FIX`y'_`r' M`y'_`r' S`y'_`r'
}
}
foreach y in rules  relate adverse enforce {
foreach r in A  {
sum `y'_`r' if race!=5 & race!=3 & race!=2 [aw=weight]
gen M`y'_`r'=r(mean)
gen S`y'_`r'=r(sd)
gen FIX`y'_`r'=(`y'_`r'-M`y'_`r')/S`y'_`r'
replace `y'_`r'=FIX`y'_`r' if race!=5 & race!=3 & race!=2
drop FIX`y'_`r' M`y'_`r' S`y'_`r'
}
}

foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in B  {
replace `y'=`y'_`r' if race==2
}
}
foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in H  {
replace `y'=`y'_`r' if race==3
}
}
foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in W  {
replace `y'=`y'_`r' if race==5
}
}
foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in A  {
replace `y'=`y'_`r' if race!=5 & race!=3 & race!=2
}
}

**MODEL effects by race/ethnicity--using group-specific factor structure
sum comp_mental_health relate rules enforce adverse

gen a=relate
gen b=rules
gen c=enforce
gen d=adverse
pwcorr  a b  [aw=weight]
gen rab=r(rho)
pwcorr  a c  [aw=weight]
gen rac=r(rho)
pwcorr  a d  [aw=weight]
gen rad=r(rho)
pwcorr  b c  [aw=weight]
gen rbc=r(rho)
pwcorr  b d  [aw=weight]
gen rbd=r(rho)
pwcorr  c d  [aw=weight]
gen rcd=r(rho)

forval i=1/9 {
foreach x in se b {
gen `x'`i'=.
}
}

*RACE specific models
foreach x in  hispanic white  {
*M1--combined
reg comp_mental_health relate rules enforce adverse  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[relate]+_b[rules]+_b[enforce]+_b[adverse] + ///
2*( (rab*_b[relate]*_b[rules]) + ///
(rac*_b[relate]*_b[enforce]) + ///
(rad*_b[relate]*_b[adverse]) + ///
(rbc*_b[rules]*_b[enforce]) + ///
(rbd*_b[rules]*_b[adverse]) + ///
(rcd*_b[enforce]*_b[adverse])) 
matrix b = r(b)
matrix V = r(V)
replace se1 = sqrt(V[1,1])  if race_string=="`x'"
replace b1 = b[1,1]  if race_string=="`x'"

*M2--Rules
reg comp_mental_health relate rules enforce adverse  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[rules]
matrix b = r(b)
matrix V = r(V)
replace se2 = sqrt(V[1,1]) if race_string=="`x'"
replace b2 = b[1,1] if race_string=="`x'"

*M3-Enforce
reg comp_mental_health relate rules enforce adverse  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[enforce]
matrix b = r(b)
matrix V = r(V)
replace se3 = sqrt(V[1,1]) if race_string=="`x'"
replace b3 = b[1,1] if race_string=="`x'"

*M4-adverse
reg comp_mental_health relate rules enforce adverse  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[adverse]
matrix b = r(b)
matrix V = r(V)
replace se4 = sqrt(V[1,1]) if race_string=="`x'"
replace b4 = b[1,1] if race_string=="`x'"

*M5-Relate
reg comp_mental_health relate rules enforce adverse  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[relate]
matrix b = r(b)
matrix V = r(V)
replace se5 = sqrt(V[1,1]) if race_string=="`x'"
replace b5 = b[1,1] if race_string=="`x'"

*M6-Rules on relate
reg relate rules  enforce adverse  $controls if race_string=="`x'"  [aw=weight]
nlcom _b[rules]
matrix b = r(b)
matrix V = r(V)
replace se6 = sqrt(V[1,1]) if race_string=="`x'"
replace b6 = b[1,1] if race_string=="`x'"

*M7-Enforce on relate
reg relate rules  enforce adverse  $controls if race_string=="`x'"  [aw=weight]
nlcom _b[enforce]
matrix b = r(b)
matrix V = r(V)
replace se7 = sqrt(V[1,1]) if race_string=="`x'"
replace b7 = b[1,1] if race_string=="`x'"

*M8adverse on relate
reg relate rules  enforce adverse  $controls if race_string=="`x'"  [aw=weight]
nlcom _b[adverse]
matrix b = r(b)
matrix V = r(V)
replace se8 = sqrt(V[1,1]) if race_string=="`x'"
replace b8 = b[1,1] if race_string=="`x'"

*M9--combined effect on relationships
reg relate rules  enforce adverse  $controls if race_string=="`x'"  [aw=weight]
nlcom _b[rules]+_b[enforce]+_b[adverse] + ///
2*( (rbc*_b[rules]*_b[enforce]) + ///
(rbd*_b[rules]*_b[adverse]) + ///
(rcd*_b[enforce]*_b[adverse])) 
matrix b = r(b)
matrix V = r(V)
replace se9 = sqrt(V[1,1])  if race_string=="`x'"
replace b9 = b[1,1]  if race_string=="`x'"
}

*Black specific models
foreach x in  black  {
*M1--combined
reg comp_mental_health relate rules enforce  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[relate]+_b[rules]+_b[enforce] + ///
2*( (rab*_b[relate]*_b[rules]) + ///
(rac*_b[relate]*_b[enforce]) + ///
(rbc*_b[rules]*_b[enforce])) 
matrix b = r(b)
matrix V = r(V)
replace se1 = sqrt(V[1,1])  if race_string=="`x'"
replace b1 = b[1,1]  if race_string=="`x'"

*M2--Rules
reg comp_mental_health relate rules enforce  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[rules]
matrix b = r(b)
matrix V = r(V)
replace se2 = sqrt(V[1,1]) if race_string=="`x'"
replace b2 = b[1,1] if race_string=="`x'"

*M3-Enforce
reg comp_mental_health relate rules enforce   $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[enforce]
matrix b = r(b)
matrix V = r(V)
replace se3 = sqrt(V[1,1]) if race_string=="`x'"
replace b3 = b[1,1] if race_string=="`x'"

*M4-adverse
replace se4 = . if race_string=="`x'"
replace b4 = . if race_string=="`x'"

*M5-Relate
reg comp_mental_health relate rules enforce  $controls  if race_string=="`x'"  [aw=weight]
nlcom _b[relate]
matrix b = r(b)
matrix V = r(V)
replace se5 = sqrt(V[1,1]) if race_string=="`x'"
replace b5 = b[1,1] if race_string=="`x'"

*M6-Rules on relate
reg relate rules  enforce   $controls if race_string=="`x'"  [aw=weight]
nlcom _b[rules]
matrix b = r(b)
matrix V = r(V)
replace se6 = sqrt(V[1,1]) if race_string=="`x'"
replace b6 = b[1,1] if race_string=="`x'"

*M7-Enforce on relate
reg relate rules  enforce   $controls if race_string=="`x'"  [aw=weight]
nlcom _b[enforce]
matrix b = r(b)
matrix V = r(V)
replace se7 = sqrt(V[1,1]) if race_string=="`x'"
replace b7 = b[1,1] if race_string=="`x'"

*M8adverse on relate
replace se8 = . if race_string=="`x'"
replace b8 = . if race_string=="`x'"


*M9--combined effect on relationships
reg relate rules  enforce   $controls if race_string=="`x'"  [aw=weight]
nlcom _b[rules]+_b[enforce] + ///
2*( (rbc*_b[rules]*_b[enforce])) 
matrix b = r(b)
matrix V = r(V)
replace se9 = sqrt(V[1,1])  if race_string=="`x'"
replace b9 = b[1,1]  if race_string=="`x'"
}

*************
***POOLED***
*Re-standardize to pooled sample
*********
foreach x in comp_mental_health $f1 $f2 $f3 $f4 {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
}

drop rules enforce relate adverse
egen rules=rowmean(B10_2	B10_6	B10_3	B10_1	B10_5	B10_19	B10_4	B10_21	B10_7)
egen enforce=rowmean(REB10_14	REB10_17 REB10_15	REB10_13	B10_16	B10_18)
egen relate=rowmean(B6_overall_parent_ch B11B	REB11A	REB10_8	C19_parent_love	B9_relationship_othe)
egen adverse=rowmean(C24_safety	NC21_angry	NC23a_rejection	NC23_left	NC20_alcohol)

foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in B  {
replace `y'=`y'_`r' if race==2
}
}
foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in H  {
replace `y'=`y'_`r' if race==3
}
}
foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in W  {
replace `y'=`y'_`r' if race==5
}
}
foreach y in rules  relate adverse enforce comp_mental_health {
foreach r in A  {
replace `y'=`y'_`r' if race!=5 & race!=3 & race!=2
}
}

foreach x in rules enforce  relate adverse  {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
} 
label var rules "Authoritative regulation"
label var enforce "Enforcement strength"
label var relate "Relationship quality"
label var adverse "Absence of traumatic experiences"

drop a b c d rab rac rad rbc rbd rcd
gen a=relate
gen b=rules
gen c=enforce
gen d=adverse
pwcorr  a b  [aw=weight]
gen rab=r(rho)
pwcorr  a c  [aw=weight]
gen rac=r(rho)
pwcorr  a d  [aw=weight]
gen rad=r(rho)
pwcorr  b c  [aw=weight]
gen rbc=r(rho)
pwcorr  b d  [aw=weight]
gen rbd=r(rho)
pwcorr  c d  [aw=weight]
gen rcd=r(rho)

**
**Pooled sample**
***************
reg comp_mental_health relate rules enforce adverse  $controls    [aw=weight]
nlcom _b[relate]+_b[rules]+_b[enforce]+_b[adverse] + ///
2*( (rab*_b[relate]*_b[rules]) + ///
(rac*_b[relate]*_b[enforce]) + ///
(rad*_b[relate]*_b[adverse]) + ///
(rbc*_b[rules]*_b[enforce]) + ///
(rbd*_b[rules]*_b[adverse]) + ///
(rcd*_b[enforce]*_b[adverse])) 
matrix b = r(b)
matrix V = r(V)
gen Ase1 = sqrt(V[1,1]) 
gen Ab1 = b[1,1]  

*M2--Rules
reg comp_mental_health relate rules enforce adverse  $controls    [aw=weight]
nlcom _b[rules]
matrix b = r(b)
matrix V = r(V)
gen Ase2 = sqrt(V[1,1]) 
gen Ab2 = b[1,1] 

*M3-Enforce
reg comp_mental_health relate rules enforce adverse  $controls    [aw=weight]
nlcom _b[enforce]
matrix b = r(b)
matrix V = r(V)
gen Ase3 = sqrt(V[1,1]) 
gen Ab3 = b[1,1] 

*M4-adverse
reg comp_mental_health relate rules enforce adverse  $controls    [aw=weight]
nlcom _b[adverse]
matrix b = r(b)
matrix V = r(V)
gen Ase4 = sqrt(V[1,1]) 
gen Ab4 = b[1,1] 

*M5-Relate
reg comp_mental_health relate rules enforce adverse  $controls    [aw=weight]
nlcom _b[relate]
matrix b = r(b)
matrix V = r(V)
gen Ase5 = sqrt(V[1,1]) 
gen Ab5 = b[1,1] 

*M6-Rules on relate
reg relate rules  enforce adverse  $controls   [aw=weight]
nlcom _b[rules]
matrix b = r(b)
matrix V = r(V)
gen Ase6 = sqrt(V[1,1]) 
gen Ab6 = b[1,1] 

*M7-Enforce on relate
reg relate rules  enforce adverse  $controls   [aw=weight]
nlcom _b[enforce]
matrix b = r(b)
matrix V = r(V)
gen Ase7 = sqrt(V[1,1]) 
gen Ab7 = b[1,1] 

*M8adverse on relate
reg relate rules  enforce adverse  $controls   [aw=weight]
nlcom _b[adverse]
matrix b = r(b)
matrix V = r(V)
gen Ase8 = sqrt(V[1,1]) 
gen Ab8 = b[1,1] 

*M9--combined effect on relationships
reg relate rules  enforce adverse  $controls   [aw=weight]
nlcom _b[rules]+_b[enforce]+_b[adverse] + ///
2*( (rbc*_b[rules]*_b[enforce]) + ///
(rbd*_b[rules]*_b[adverse]) + ///
(rcd*_b[enforce]*_b[adverse])) 
matrix b = r(b)
matrix V = r(V)
gen Ase9 = sqrt(V[1,1]) 
gen Ab9 = b[1,1]  


**Models to estimate R^2
reg comp_mental_health relate rules enforce adverse  $controls   [aw=weight]
test $controls
gen ar1=e(r2_a)
reg comp_mental_health relate rules enforce   $controls  if race_string=="black"  [aw=weight]
gen ar2=e(r2_a)
reg comp_mental_health relate rules enforce adverse  $controls  if race_string=="hispanic"  [aw=weight]
gen ar3=e(r2_a)
reg comp_mental_health relate rules enforce adverse  $controls  if race_string=="white"  [aw=weight]
gen ar4=e(r2_a)

reg relate rules enforce adverse  $controls   [aw=weight]
gen ar5=e(r2_a)
reg relate rules enforce   $controls  if race_string=="black"  [aw=weight]
gen ar6=e(r2_a)
reg relate rules enforce adverse  $controls  if race_string=="hispanic"  [aw=weight]
gen ar7=e(r2_a)
reg relate rules enforce adverse  $controls  if race_string=="white"  [aw=weight]
gen ar8=e(r2_a)

*Results for Table 6
sum ar*
collapse (mean) ar1 ar2 ar3 ar4 ar5 ar6 ar7 ar8 Ase1 Ab1 Ase2 Ab2 Ase3 Ab3 Ase4 Ab4 Ase5 Ab5 Ase6 Ab6 Ase7 Ab7 Ase8 Ab8 Ab9 Ase9 b1 b2 b3 b4 b5 b6 b7 b8 b9 se1 se2 se3 se4 se5 se6 se7 se8 se9, by(race_string)

drop if race_string=="other" 
replace race_string="all" if race_string==""


forval i=1/9 {
gen up`i'=b`i'+(1.96*se`i')
gen low`i'=b`i'-(1.96*se`i')
gen upA`i'=Ab`i'+(1.96*Ase`i')
gen lowA`i'=Ab`i'-(1.96*Ase`i')
}

encode race_string, gen(race_num)
tab race_string


label define label_rs 1 "All groups" 2 "Black" 3 "Hispanic" 4 "White" 5 " ",  add 
label values race_num label_rs
levelsof race_num, local(rval)

cd "${output}"

forval i=1/9 {
replace b`i'=Ab`i' if race_num==1
replace up`i'=upA`i' if race_num==1
replace low`i'=lowA`i' if race_num==1
}

gen race_num1=race_num
gen race_num2=race_num+.15
gen race_num3=race_num+.2
gen race_num4=race_num+.3
gen race_num5=race_num+.4

tab race_num
twoway (scatter b1 race_num ,  lwidth(thick) color(green*.7) fintensity(25) ///
mlabel(b1) mlabc(black) msize(medium) mlabsize(small) msymbol(D) mlabposition(1) mlabformat(%9.2f)) ///
|| (rcap up1 low1 race_num, lpattern(dash) lcolor(green*.7))  ///
|| (scatter b5 race_num2,  lwidth(thick) color(magenta*.7) fintensity(25) ///
mlabel(b5) mlabc(black) msize(medium) mlabsize(small) msymbol(Sh) mlabposition(4) mlabformat(%9.2f)) ///
|| (rcap up5 low5 race_num2, lpattern(dash) lcolor(magenta*.7))  ///
|| (scatter b2 race_num3,  lwidth(thick) color(blue*.7) fintensity(25) ///
mlabel(b2) mlabc(black) msize(medium) mlabsize(small) msymbol(O) mlabposition(11) mlabformat(%9.2f)) ///
|| (rcap up2 low2 race_num3, lpattern(dash) lcolor(blue*.7))  ///
|| (scatter b3 race_num4,  lwidth(thick) color(red*.7) fintensity(25) ///
mlabel(b3) mlabc(black) msize(medium) mlabsize(small) msymbol(Th) mlabposition(12) mlabformat(%9.2f)) ///
|| (rcap up3 low3 race_num4, lpattern(dash) lcolor(red*.7)) ///
|| (scatter b4 race_num5,  lwidth(thick) color(orange*.7) fintensity(25) ///
mlabel(b4) mlabc(black) msize(medium) mlabsize(small) msymbol(+) mlabposition(5) mlabformat(%9.2f)) ///
|| (rcap up4 low4 race_num5, lpattern(dash) lcolor(orange*.7)),  ///
legend(row(1) order(1 "Mean" 2 "95% CI") size(small)) ///
plotregion(color(white)) graphregion(color(white))  ///
xlabel(`rval', valuelabels angle(0) labsize(small)) ///
xscale(titlegap(medium)) xsc(r(1 5))   ///
yscale(titlegap(medium))  ///
yline(0) ///
ylabel(, format(%9.1f)) ///
legend(order(1 "Combined effect" 3 "Relationship quality" 5 "Responsiveness" 7 "Demandingness" 9 "No adverse experiences") size(small) position(6) rows(2) cols(3) region(lwidth(none)) ) ///
ylabel(0(.40)1.2, grid gmax labgap(tiny)) ///
xtitle("Parental race/ethnicity", size(medsmall)) ////
saving(race_model1_unique, replace)  ///
ytitle("Effect-size", size(medsmall)) ////
title("Effects on mental-health, group-specific", size(medsmall) span) 

twoway (scatter b9 race_num,  lwidth(thick) color(green*.7) fintensity(25) ///
mlabel(b9) mlabc(black) msize(medium) mlabsize(small) msymbol(D) mlabposition(5) mlabformat(%9.2f)) ///
|| (rcap up9 low9 race_num, lpattern(dash) lcolor(green*.7))  ///
|| (scatter b6 race_num2,  lwidth(thick) color(blue*.7) fintensity(25) ///
mlabel(b6) mlabc(black) msize(medium) mlabsize(small) msymbol(O) mlabposition(1) mlabformat(%9.2f)) ///
|| (rcap up6 low6 race_num2, lpattern(dash) lcolor(blue*.7))  ///
|| (scatter b7 race_num3,  lwidth(thick) color(red*.7) fintensity(25) ///
mlabel(b7) mlabc(black) msize(medium) mlabsize(small) msymbol(Th) mlabposition(11) mlabformat(%9.2f)) ///
|| (rcap up7 low7 race_num3, lpattern(dash) lcolor(red*.7))  ///
|| (scatter b8 race_num4,  lwidth(thick) color(orange*.7) fintensity(25) ///
mlabel(b8) mlabc(black) msize(medium) mlabsize(small) msymbol(+) mlabposition(1) mlabformat(%9.2f)) ///
|| (rcap up8 low8 race_num4, lpattern(dash) lcolor(orange*.7)),  ///
legend(row(1) order(1 "Mean" 2 "95% CI") size(small)) ///
plotregion(color(white)) graphregion(color(white))  ///
xlabel(`rval', valuelabels angle(0) labsize(small)) ///
xscale(titlegap(medium))  ///
yscale(titlegap(medium))  ///
yline(0) ///
ylabel(, format(%9.1f)) ///
legend(order(1 "Combined effect" 3 "Responsiveness" 5 "Demandingness" 7 "No adverse experiences" ) position(6) size(small) rows(2) region(lwidth(none)) ) ///
ylabel(0(.4)1.2, grid gmax labgap(tiny)) ///
xtitle("Parental race/ethnicity", size(medsmall)) ////
saving(race_model2_unique, replace)  ///
ytitle("Effect-size", size(medsmall)) ////
title("Effects on relationship-quality, group-specific", size(medsmall) span) 

gen indirect_f1=b5*(b6)
gen indirect_f2=b5*(b7)
gen direct_f1=b2
gen direct_f2=b3
gen pct_indirect_f1= indirect_f1/( indirect_f1+direct_f1)
gen pct_indirect_f2= indirect_f2/( indirect_f2+direct_f2)
export delimited race_string b1 up1 low1 indirect_f1 indirect_f2 direct_f1 direct_f2 pct_indirect_f1 pct_indirect_f2 ///
ar1 ar2 ar3 ar4 ar5 ar6 ar7 ar8 ///
using  "${output}\summary_effect_sizes_same_structure.csv", replace

graph combine race_model1_unique.gph race_model1_same.gph   ///
race_model2_unique.gph race_model2_same.gph, ///
imargin(medlarge) row(2) col(2)   iscale(*.75) ///
plotregion(color(white)) graphregion(color(white)) 
graph export "Fig_Parenting_Effects_by_Race_unique_same_comb.png", replace width(3300) height(2500) 

graph combine race_model1_unique.gph    ///
race_model2_unique.gph, ///
imargin(medlarge) row(1) col(2)   iscale(*.75) ///
plotregion(color(white)) graphregion(color(white)) 
graph export "Fig_Parenting_Effects_by_Race_unique_comb.png", replace width(3300) height(2500) 

